package com.qf.controller;

import com.qf.entity.MallGoodsCatEntity;
import com.qf.service.ICatService;
import com.qf.service.impl.CatService;
import com.qf.views.ViewCatTplEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@ResponseBody
@RequestMapping("/cat")
@Api(tags = "种类管理")
public class CatController {
    @Autowired
    private ICatService service;

    @ApiOperation("根据父id查询子级")
    @GetMapping("/findById/{pid}")
    public List<ViewCatTplEntity> findById(@ApiParam("父id") @PathVariable String pid) {
        return service.findById(pid);
    }

    @ApiOperation("查询所有种类")
    @GetMapping("/findAll")
    public List<ViewCatTplEntity> findAll() {
        return service.findAll();
    }

    @PostMapping("/add")
    @ApiOperation("新增种类")
    public Map add(@RequestBody @ApiParam("种类实体对象") MallGoodsCatEntity entity) {
        int add = service.add(entity);
        if (add == 1) {
            return new HashMap() {{
                put("res", "添加成功");
            }};
        }
        return new HashMap() {{
            put("res", "添加失败");
        }};
    }

    @ApiOperation("种类修改")
    @PostMapping("/update")
    public Map update(@RequestBody @ApiParam("种类实体类对象") MallGoodsCatEntity entity) {
        int update = service.update(entity);
        if (update == 1) {
            return new HashMap() {{
                put("res", "修改成功");
            }};
        }
        return new HashMap() {{
            put("res", "修改失败");
        }};
    }
}